package com.Iron.day1;

import java.sql.Connection;
import java.sql.DriverManager;
import java.util.logging.Logger;

public class DemoThreadLocal {


    public static void main(String[] args) {
        test();
    }

    private static void test() {


        for (int i = 0; i < 5; i++) {
            new Thread(()->{
                System.out.println(Utils.getConnection().toString());
                System.out.println(Utils.getConnection().toString());
                System.out.println(Utils.getConnection().toString());
            }, "i" + (i+1)).start();
        }
    }

    static class Utils{

        private static final  ThreadLocal<Connection> threadLocal = new ThreadLocal<Connection>();

        public static Connection getConnection(){
            Connection con = threadLocal.get();
            if (con==null) {
                con = getIn();
                threadLocal.set(con);
            }
            return con;
        }


        public static Connection getIn(){

            try {
                return DriverManager.getConnection("jdbc:mysql://localhost:3300/blog?useSSL=false","root","root");

            }catch (Exception e){
                throw new RuntimeException(e);
            }
        }
    }


}
